package com.jai.pms.message.config;

import com.jai.pms.message.listener.RedisStreamListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ExecutorConfig {
    private static Logger logger = LoggerFactory.getLogger(RedisStreamListener.class);

    @Bean
    public Executor executor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(getCount());
        executor.setMaxPoolSize(getCount());
        executor.setAllowCoreThreadTimeOut(true);
        executor.setQueueCapacity(50);
        executor.setThreadNamePrefix("jai-stream-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
        logger.debug("任务线程池数量：{}", getCount());
        return executor;
    }

    public int getCount() {
        int core = Runtime.getRuntime().availableProcessors();
        return core * 2;
    }
}
